package main

import (
	"encoding/json"
	"fmt"
	"github.com/graarh/golang-socketio"
	"github.com/graarh/golang-socketio/transport"
	"time"
)
type ocsExt struct {
	IsLogin string `json:"isLogin,omitempty"`
}
type ocsConncet struct {
	Telephone			string				`json:"telephone,omitempty"`
	AccessSource 		string				`json:"accessSource,omitempty"`
	Directtransman		string				`json:"directtransman,omitempty"`
	IsForce				string				`json:"isForce,omitempty"`
	IsAnonymous			string				`json:"isAnonymous,omitempty"`
	AccessTypeDetail	string				`json:"accessTypeDetail,omitempty"`
	Client				string				`json:"client,omitempty"`
	Ext					ocsExt 				`json:"ext"`
}
//	content: "<p><br/>1111</p>", receiver: "", name: "", accessType: "3", client: "web"}

type ocsMessage struct {
	Content				string				`json:"content"`
	Receiver			string				`json:"receiver"`
	AccessType			int					`json:"accessType"`
	Client				string				`json:"client"`
	Name				string				`json:"name"`
	ChatId				string				`json:"chatId,omitempty"`
	MsgId				string				`json:"msgId,omitempty"`
	Sender				string				`json:"sender,omitempty"`
	SenderType			int					`json:"senderType,omitempty"`
}
type oscProps map[string]interface{}
type ocsModeRespnse struct {
	UserId				string				`json:"userId,omitempty"`
	CurrentModeType		int				`json:"currentModeType,omitempty"`
	Name				string				`json:"name,omitempty"`
	MsgContent			string				`json:"msgContent,omitempty"`
	//Props				oscProps			`json:"props,omitempty"`
}
type ocsModeRequest struct {
	Telephone			string				`json:"telephone,omitempty"`
	AccessSource 		string				`json:"accessSource,omitempty"`
	Directtransman		string				`json:"directtransman,omitempty"`
	IsForce				string				`json:"isForce,omitempty"`
	IsAnonymous			string				`json:"isAnonymous,omitempty"`
	AccessTypeDetail	string				`json:"accessTypeDetail,omitempty"`
	Client				string				`json:"client,omitempty"`
	Ext					ocsExt 				`json:"ext,omitempty"`
	ToMode				string 				`json:"toMode,omitempty"`
}
type ocsMode struct {
	ocsModeRequest
	ocsModeRespnse
}
func main() {
	ch :=make(chan int,1)
	c, err := gosocketio.Dial("ws://10.21.19.13:9095/socket.io/?X-OCS=asiainfo&EIO=3&transport=websocket", transport.GetDefaultWebsocketTransport())
	fmt.Println(c,err)
	var mode ocsMode
	str:=`{"userId":"c38d9bb6-40c1-4eb7-9638-65d6751b5cd0","currentModeType":3,"name":"","msgContent":"\\u6B63\\u5728\\u8F6C\\u63A5\\u4EBA\\u5DE5\\u670D\\u52A1\\uFF0C\\u8BF7\\u7A0D\\u5019","props":{"abc":"111"}}`
	json.Unmarshal([]byte(str),&mode)
	fmt.Println(mode)
	c.On("ocs_message",func(sio *gosocketio.Channel, msg ocsMessage) {
		msgBytes,_:=json.Marshal(msg)
		fmt.Println(string(msgBytes))
		ch<-1
	})

	c.On("ocs_mode",func(sio *gosocketio.Channel, msg ocsMessage) {
		msgBytes,_:=json.Marshal(msg)
		fmt.Println(string(msgBytes))
	})


	msg1:=&ocsConncet{
		Telephone: "13585188937",
		AccessSource:"3",
		Directtransman:"0",
		IsForce:"0",
		IsAnonymous:"0",
		AccessTypeDetail:"8",
		Client:"web",Ext:ocsExt{IsLogin:"1"}}
	if err=c.Emit("ocs_connect",msg1);err!=nil{
		fmt.Println(err)
	}

	fmt.Println("你好")
	time.Sleep(5*time.Second)
	msg2:=&ocsMessage{Client:"client",AccessType:3,Content:"你好"}
	if err=c.Emit("ocs_message",msg2);err!=nil{
		fmt.Println(err)
	}

	//msg3:=&ocsMode{
	//	Client:"wechat",
	//	IsForce:"0",
	//	Telephone:"13585188937",
	//	ToMode:"1"}
	//
	//if err=c.Emit("ocs_mode",msg3);err!=nil{
	//	fmt.Println(err)
	//}
	<-ch
	c.Close()
	}
